//
//  FeedbackViewController.m
//  eshow
//
//  Created by Hisoka on 15/11/13.
//  Copyright © 2015年 Hisoka. All rights reserved.
//

#import "FeedbackViewController.h"

@interface FeedbackViewController ()<UITextViewDelegate>

@property (nonatomic, strong) UITextView *feedBackTV;
@property (nonatomic, strong) UILabel *feedBackLB;

@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customizeBackItem];
    self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self setStyle];
}

- (void) setStyle {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    self.navigationItem.title = @"意见反馈";
    self.view.backgroundColor = GRAY_COLOR;
    
    self.feedBackTV = [UITextView newAutoLayoutView];
    [self.view addSubview:_feedBackTV];
    
    [_feedBackTV autoPinToTopLayoutGuideOfViewController:self withInset:20];
    [_feedBackTV autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.view];
    [_feedBackTV autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 200)];
    _feedBackTV.backgroundColor = WHITE_COLOR;
    _feedBackTV.returnKeyType = UIReturnKeyDone;
    _feedBackTV.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _feedBackTV.delegate = self;
    _feedBackTV.font = [UIFont systemFontOfSize:17.0f];
    
    self.feedBackLB = [UILabel newAutoLayoutView];
    [self.view addSubview:_feedBackLB];
    
    [_feedBackLB autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:_feedBackTV withOffset:10];
    [_feedBackLB autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:_feedBackTV withOffset:5];
    [_feedBackLB autoSetDimensionsToSize:CGSizeMake(ScreenWidth, 20)];
    _feedBackLB.text = TEXT_FEEDBACK_PLACEHOLDER;
    _feedBackLB.textColor = GRAY_COLOR;

    UIButton *reqBTN = [UIButton newAutoLayoutView];
    [self.view addSubview:reqBTN];
    
    [reqBTN autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_feedBackTV withOffset:40];
    [reqBTN autoAlignAxis:ALAxisVertical toSameAxisOfView:self.view];
    [reqBTN autoSetDimensionsToSize:CGSizeMake(300, 42)];
    [reqBTN setBackgroundImage:[UIImage imageNamed:@"confirmBTN"] forState:UIControlStateNormal];
    [reqBTN addTarget:self action:@selector(reqFeedBackAction) forControlEvents:UIControlEventTouchUpInside];

}

- (void) reqFeedBackAction {
    
    [MobClick event:@"Click_Feedback_confirmBTN"];
    
    if (_feedBackTV.text.length <= 0) {
        [SVProgressHUD showInfoWithStatus:@"请填写反馈内容"];
        return;
    }
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:[[TMCache sharedCache] objectForKey:@"SSID"] forKey:@"ssid"];
    [parameters setObject:_feedBackTV.text forKey:@"content"];
    
    [SVProgressHUD show];
    [[HttpManager instance] requestWithMethod:@"Util/feedback"
                                   parameters:parameters
                                      success:^(NSDictionary *result) {
                                          
                                          [_feedBackTV resignFirstResponder];
                                          
                                          [SVProgressHUD showSuccessWithStatus:@"反馈成功" maskType:SVProgressHUDMaskTypeBlack];
                                          
                                          [_feedBackTV setText:@""];
                                          
                                      }
                                      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                          [SVProgressHUD showInfoWithStatus:[error localizedDescription] maskType:SVProgressHUDMaskTypeBlack];
                                      }];
}

- (void) textViewDidChange:(UITextView *)textView {

    if (textView == _feedBackTV) {
        if (textView.text.length == 0) {
            _feedBackLB.hidden = NO;
        }else{
            _feedBackLB.hidden = YES;
        }
    }
}

- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }else{
        return YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
